﻿using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics.Eventing.Reader;
using System.Drawing;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace LibarayMS
{
    public partial class Frm_fine_manage : Form
    {
        private string mname,mid;
        public event myDelegate returnEvent;
        public event myDelegate logoutEvent;
        public Frm_fine_manage(string mname,string mid)
        {
            this.mname = mname;
            this.mid = mid;
            InitializeComponent();
        }

        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            returnEvent();
            this.Close();
        }

        private void linkLabel2_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            logoutEvent();
            this.Close();
        }

        private void Frm_fine_manage_Load(object sender, EventArgs e)
        {
            lbl_mname.Text = mname;
            cb_head.SelectedIndex = 0;
            MySqlConnection conn = new MySqlConnection("Data Source=localhost;database=my_db1;UID=root;PWD=123456");
            conn.Open();
            MySqlCommand cmd = new MySqlCommand("updateFineAll", conn);
            cmd.CommandType = CommandType.StoredProcedure;
            //定义输入函数
            cmd.Parameters.AddWithValue("mid_", mid);
            //定义输出函数
            MySqlParameter result = cmd.Parameters.Add("result", MySqlDbType.Int64);
            result.Direction = ParameterDirection.Output;
            cmd.ExecuteNonQuery();
            conn.Close();
            bind_fine_manage();
        }

        private void bind_fine_manage()
        {
            MySqlConnection conn = new MySqlConnection("Data Source=localhost;database=my_db1;UID=root;PWD=123456");
            conn.Open();
            string str = "select RID,SID,ISBN,BName,author,EName,edit_ID,price,default_type,default_time,g.pay,fine_num " +
                "from reader_info a,book_info b,book_store_info c,book_edit_info d,edit_house_info e,book_date_info f,fine g,book_price_info h" +
                " where b.BRID=c.BRID and d.PHID=e.PHID and b.BRID=d.BRID and b.BRID=f.BRID and a.RRID=g.RRID and c.SRID=g.SRID and h.BRID=b.BRID;";
            MySqlDataAdapter msda = new MySqlDataAdapter(str, conn);
            DataTable dt = new DataTable();
            msda.Fill(dt);
            conn.Close();
            listView1.Items.Clear();
            foreach (DataRow dr in dt.Rows)
            {
                ListViewItem myitem = new ListViewItem(dr["RID"].ToString());
                myitem.SubItems.Add(dr["SID"].ToString());
                myitem.SubItems.Add(dr["ISBN"].ToString());
                myitem.SubItems.Add(dr["BName"].ToString());
                myitem.SubItems.Add(dr["author"].ToString());
                myitem.SubItems.Add(dr["EName"].ToString());
                myitem.SubItems.Add(dr["edit_ID"].ToString());
                myitem.SubItems.Add(dr["price"].ToString());
                if (dr["default_type"].ToString() == "0")
                {
                    myitem.SubItems.Add("超期");
                }
                else
                {
                    myitem.SubItems.Add("损坏");
                }
                myitem.SubItems.Add(Convert.ToDateTime(dr["default_time"]).ToString("yyyy-MM-dd"));
                if (dr["pay"].ToString() == "False")
                {
                    myitem.SubItems.Add("未交款");
                }
                else
                {
                    myitem.SubItems.Add("已交款");
                }
                myitem.SubItems.Add(dr["fine_num"].ToString());
                listView1.Items.Add(myitem);
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            DialogResult dResult = MessageBox.Show(string.Format("确认支付{0}？", lbl_fine.Text), "支付提示", MessageBoxButtons.YesNo);
            if (dResult == DialogResult.Yes)
            {
                MySqlConnection conn = new MySqlConnection("Data Source=localhost;database=my_db1;UID=root;PWD=123456");
                conn.Open();
                MySqlCommand cmd = new MySqlCommand("payFine", conn);
                cmd.CommandType = CommandType.StoredProcedure;
                string rid = listView1.SelectedItems[0].SubItems[0].Text;
                string sid = listView1.SelectedItems[0].SubItems[1].Text;
                //定义输入函数
                cmd.Parameters.AddWithValue("rid_", rid);
                cmd.Parameters.AddWithValue("sid_", sid);
                cmd.Parameters.AddWithValue("mid_", mid);
                //定义输出函数
                MySqlParameter result = cmd.Parameters.Add("result", MySqlDbType.Int64);
                result.Direction = ParameterDirection.Output;
                cmd.ExecuteNonQuery();
                conn.Close();
                if (result.Value.ToString() == "1")
                {
                    MessageBox.Show("恭喜你，支付成功！");
                    clearTextBox();
                }
                else
                {
                    MessageBox.Show("对不起，支付失败！");
                }
                bind_fine_manage();
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            string rid = txt_rid.Text.Trim();
            string isbn = "", sid = "", bname = "";
            if (cb_head.SelectedIndex == 0) { bname = txt_book.Text.Trim(); }
            else if (cb_head.SelectedIndex == 1) { isbn = txt_book.Text.Trim(); }
            else { sid = txt_book.Text.Trim(); }
            MySqlConnection conn = new MySqlConnection("Data Source=localhost;database=my_db1;UID=root;PWD=123456");
            conn.Open();
            string str = string.Format("select RID, SID, ISBN, BName, author, EName, edit_ID, price, default_type, default_time, g.pay, fine_num " +
                "from reader_info a,book_info b,book_store_info c,book_edit_info d,edit_house_info e,book_date_info f,fine g,book_price_info h" +
                " where b.BRID=c.BRID and d.PHID=e.PHID and b.BRID=d.BRID and b.BRID=f.BRID and a.RRID=g.RRID and c.SRID=g.SRID and h.BRID=b.BRID " +
                "and RID like '%{0}%' and BName like '%{1}%' and ISBN like '%{2}%' and SID like'%{3}%';", rid, bname, isbn, sid);
            MySqlDataAdapter msda = new MySqlDataAdapter(str, conn);
            DataTable dt = new DataTable();
            msda.Fill(dt);
            conn.Close();
            listView1.Items.Clear();
            foreach (DataRow dr in dt.Rows)
            {
                ListViewItem myitem = new ListViewItem(dr["RID"].ToString());
                myitem.SubItems.Add(dr["SID"].ToString());
                myitem.SubItems.Add(dr["ISBN"].ToString());
                myitem.SubItems.Add(dr["BName"].ToString());
                myitem.SubItems.Add(dr["author"].ToString());
                myitem.SubItems.Add(dr["EName"].ToString());
                myitem.SubItems.Add(dr["edit_ID"].ToString());
                myitem.SubItems.Add(dr["price"].ToString());
                if (dr["default_type"].ToString() == "0")
                {
                    myitem.SubItems.Add("超期");
                }
                else
                {
                    myitem.SubItems.Add("损坏");
                }
                myitem.SubItems.Add(Convert.ToDateTime(dr["default_time"]).ToString("yyyy-MM-dd"));
                if (dr["pay"].ToString() == "False")
                {
                    myitem.SubItems.Add("未交款");
                }
                else
                {
                    myitem.SubItems.Add("已交款");
                }
                myitem.SubItems.Add(dr["fine_num"].ToString());
                listView1.Items.Add(myitem);
            }
        }

        private void cb_head_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listView1.SelectedItems.Count > 0)
            {
                ListViewItem myitem = listView1.SelectedItems[0];
                if (cb_head.SelectedIndex == 0) { txt_book.Text = myitem.SubItems[3].Text; }
                else if (cb_head.SelectedIndex == 1) { txt_book.Text = myitem.SubItems[2].Text; }
                else { txt_book.Text = myitem.SubItems[1].Text; }
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            clearTextBox();
            bind_fine_manage();
        }

        private void clearTextBox()
        {
            txt_book.Text = string.Empty;
            txt_rid.Text = string.Empty;
            lbl_fine.Text = "元";
            cb_head.SelectedIndex = 0;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            clearTextBox();
        }

        private void listView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            lbl_fine.Text = listView1.SelectedItems[0].SubItems[11].Text+"元";
            ListViewItem myitem = listView1.SelectedItems[0];
            txt_rid.Text = myitem.SubItems[0].Text;
            if (cb_head.SelectedIndex == 0) { txt_book.Text = myitem.SubItems[2].Text; }
            else if (cb_head.SelectedIndex == 1) { txt_book.Text = myitem.SubItems[3].Text; }
            else { txt_book.Text = myitem.SubItems[1].Text; }
        }
    }
}
